<!DOCTYPE html>

<html class="no-js">

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title></title>
  <meta name="description" content="" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />

  <script src="https://cdn.staticfile.org/axios/0.19.0-beta.1/axios.js"></script>

  <script src="../../cdn/vue/vue.global.min.js"></script>
  <link rel="stylesheet" href="../../cdn/element-plus/index.css" />
  <script src="../../cdn/element-plus/index.full.min.js"></script>
  <link rel="stylesheet" href="../../../lib/index.css" />
  <script src="../../../lib/avue.js"></script>
</head>

<body>
  <div id="app">
    <avue-crud ref="crud" :option="option" :data="data" v-model:page="page" @on-load="onLoad"
      @expand-change="expandChange" @sort-change="sortChange">
      <template #expand="scope">
        {{scope}}
      </template>
      <template #switch="scope">
        <el-switch v-model="scope.row.switch" active-color="#13ce66" inactive-color="#ff4949" :active-value="0"
          :inactive-value="1">
        </el-switch>
      </template>
    </avue-crud>
  </div>
</body>
<script>

  var app = Vue.createApp({
    data() {
      return {
        page: {
          pageSize: 20,
          total: 0
        },
        expands: [],
        data: []
      }
    },
    computed: {
      option() {
        return {
          dialogType: 'drawer',
          expand: true,
          viewBtn: true,
          border: true,
          index: true,
          // defaultExpandAll: true,
          expandRowKeys: this.expands,
          column: [{
            label: '开关',
            prop: 'switch'
          }, {
            label: '多行文本',
            prop: 'textarea',
            type: "textarea",
            search: true
          }, {
            label: '下拉框',
            prop: 'select',
            type: "select",
            dicData: [{
              label: '一级0',
              value: 0
            }, {
              label: '一级1',
              value: 1,
            }, {
              label: '一级2',
              value: 2,
            }, {
              label: '一级2',
              value: 3,
            }, {
              label: '一级2',
              value: 4,
            }, {
              label: '一级2',
              value: 5,
            }, {
              label: '一级2',
              value: 6,
            }, {
              label: '一级2',
              value: 7,
            }],
          }, {
            label: '多选',
            prop: 'checkbox',
            type: 'checkbox',
            dicData: [{
              label: '一级0',
              value: 0,
            }, {
              label: '一级1',
              value: 1,
            }, {
              label: '一级2',
              value: 2,
            }]
          }, {
            label: '级别',
            prop: 'cascader',
            type: 'tree',
            multiple: true,
            searchMultiple: true,
            search: true,
            dicData: [{
              label: '一级',
              value: 0,
              children: [
                {
                  label: '一级1',
                  value: 1,
                }, {
                  label: '一级2',
                  value: 2,
                  children: [
                    {
                      label: '一级1',
                      value: 1,
                      children: [
                        {
                          label: '一级1',
                          value: 1,
                          children: [
                            {
                              label: '一级1',
                              value: 1,
                            }, {
                              label: '一级3',
                              value: 2,
                            }
                          ]
                        }, {
                          label: '一级2',
                          value: 2,
                        }
                      ]
                    }, {
                      label: '一级2',
                      value: 2,
                    }
                  ]
                }
              ]
            }],
          }, {
            label: '姓名',
            prop: 'name'
          }, {
            label: '年龄',
            prop: 'sex',
            sortable: true,
          }]
        }
      }
    },
    mounted() {
      //调用方法展开
      // this.$refs.crud.toggleRowExpansion(this.data[0], true)
    },
    methods: {
      onLoad(page) {
        this.page.total = 40
        this.data = [{
          id: 1,
          name: '张三',
          switch: 1,
          checkbox: [0, 1],
          cascader: [0, 1],
          sex: 12,
        }, {
          id: 2,
          name: '李四',
          switch: 0,
          checkbox: [0, 2],
          cascader: [0, 2],
          sex: 20,
        }]
      },
      sortChange(val) {
        this.page.pageSize = 50;
        this.onLoad(this.page, this.params);
      },
      expandChange(row, expendList) {
        if (expendList.length) {
          this.expands = []
          if (row) {
            this.expands.push(row.id)
          }
        } else {
          this.expands = []
        }
        this.$message.success('展开回调')
      },
    }
  })
  app.use(ElementPlus)
  app.use(AVUE)
  app.mount('#app')
</script>

</html>